package com.kly.user.db.dao.impl;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Maps;
import com.kly.user.db.dao.UserBehaviorGpsDao;
import com.kly.user.db.entity.UserBehaviorGpsEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* User: linqu
* Date: 2023-05-24
* Time: 10:10:36.849
*/

@Repository
public class UserBehaviorGpsDaoImpl extends AbstractBaseMapper<UserBehaviorGpsEntity> implements UserBehaviorGpsDao {

    @Override
    public UserBehaviorGpsEntity getLastGps(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectOne(this.getStatement("getLastGps"), params);
    }

    @Override
    public UserBehaviorGpsEntity addGps(String userCode, String state, String stateAbbr, String city, String lng, String lat) {
        final UserBehaviorGpsEntity existsRecord = getLastGps(userCode);
        if (existsRecord == null) {
            UserBehaviorGpsEntity entity = new UserBehaviorGpsEntity();
            entity.setUserCode(userCode);
            entity.setState(state);
            entity.setStateAbbr(stateAbbr);
            entity.setCity(city);
            entity.setLng(lng);
            entity.setLat(lat);
            return super.insert(entity);
        }

        existsRecord.setUserCode(userCode);
        existsRecord.setState(state);
        existsRecord.setStateAbbr(stateAbbr);
        existsRecord.setCity(city);
        existsRecord.setLng(lng);
        existsRecord.setLat(lat);
        final Integer updateFlag = super.updateByPrimaryKey(existsRecord);
        if (updateFlag > 0) {
            return existsRecord;
        }
        return null;
    }

    @Override
    public List<UserBehaviorGpsEntity> queryGpsListByUserCode(List<String> list) {
        return this.getSqlSession().selectList(this.getStatement("queryGpsListByUserCode"), list);
    }

}